package com.wwdx.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wwdx.model.WwdxMerchantProductModel;
import com.wwdx.service.WwdxMerchantProductServiceI;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author liguoliang
 * @date 2018/1/31 18:54
 */
@Service
public class WwdxMerchantProductServiceImpl extends BaseServiceImpl<WwdxMerchantProductModel> implements WwdxMerchantProductServiceI {

    @Override
    public Long selectModelCount(WwdxMerchantProductModel model) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(1);
        params.put("merchant_id", model.getMerchant_id());
        String hql = "select count(1) from WwdxMerchantProductModel where merchant_id=:merchant_id";
        return this.count(hql,params);
    }

    @Override
    public List<WwdxMerchantProductModel> selectModelList(WwdxMerchantProductModel model, Integer page, Integer rows) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(1);
        String sql = "SELECT m2.merchant_name,m2.scale,m3.title,m3.img,m3.price as price1,m4.product_name," +
                        "m4.price as price2,m1.product_url,m1.product_id,m1.qrcode_url,m1.type,m1.id,m1.gmt_created \n" +
                        "FROM wwdx_merchant_product m1\n" +
                        "LEFT JOIN wwdx_merchant m2 ON m1.merchant_id = m2.id\n" +
                        "LEFT JOIN wwdx_theme m3 ON m1.product_id = m3.themeid\n" +
                        "LEFT JOIN wwdx_vip_product m4 ON m1.product_id = m4.product_id\n" +
                        "where m1.merchant_id=:merchant_id order by m1.gmt_created DESC";
        params.put("merchant_id", model.getMerchant_id());
        List list = this.findBySql(sql, params,page,rows);
        int size = list.size();
        List<WwdxMerchantProductModel> wwdxMerchantProductModels = Lists.newArrayListWithExpectedSize(size);
        for (int i = 0;i<size;i++){
            model = (WwdxMerchantProductModel) model.clone();
            Map map = (Map) list.get(i);
            model.setProduct_id(((BigInteger)map.get("product_id")).longValue());
            model.setMerchantName(Objects.isNull(map.get("merchant_name"))?"":map.get("merchant_name").toString());
            model.setQrcode_url(Objects.isNull(map.get("qrcode_url"))?"":map.get("qrcode_url").toString());
            model.setProduct_url(Objects.isNull(map.get("product_url"))?"":map.get("product_url").toString());
            model.setImg(Objects.isNull(map.get("img"))?"":map.get("img").toString());
            model.setScale(Objects.isNull(map.get("scale")) ? 0 : (int) map.get("scale"));
            model.setThemePrice(Objects.isNull(map.get("price1"))? new BigDecimal(0) :(BigDecimal) map.get("price1"));
            model.setVipPrice(Objects.isNull(map.get("price2"))? new BigDecimal(0) :(BigDecimal) map.get("price2"));
            model.setThemeProductName(Objects.isNull(map.get("title"))?"":map.get("title").toString());
            model.setVipProductName(Objects.isNull(map.get("product_name"))?"":map.get("product_name").toString());
            model.setType(Objects.isNull(map.get("type"))?0:(int) map.get("type"));
            model.setGmt_created(new DateTime(map.get("gmt_created")).toDate());
            model.setId(((BigInteger)map.get("id")).longValue());
            wwdxMerchantProductModels.add(model);
        }
        return wwdxMerchantProductModels;
    }

    @Override
    public WwdxMerchantProductModel isExits(String merchant_id, Long product_id) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("merchant_id", merchant_id);
        params.put("product_id", product_id);
        String hql = "from WwdxMerchantProductModel where merchant_id=:merchant_id and product_id=:product_id";
        return this.getByHql(hql, params);
    }

    @Override
    public List<WwdxMerchantProductModel> list(String merchant_id) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(1);
        params.put("merchant_id", merchant_id);
        String hql = "from WwdxMerchantProductModel where merchant_id =:merchant_id";
        return this.find(hql, params);
    }
}
